/**
 * Created by DExtra on 2017-8-14 16:17:30.
 * 自定义实现的测量面积方法
 */
define([
        "dojo/on",
        "dojo/_base/lang",

        "esri/geometry/Polygon",
        "esri/geometry/Point",
        "esri/geometry/geometryEngine",
        "esri/Graphic",
        "esri/symbols/SimpleFillSymbol",
        "esri/symbols/TextSymbol",
        "esri/views/2d/draw/Draw",

        "esri/core/Accessor",
],
    function (on,lang,
              Polygon, Point, geometryEngine, Graphic, SimpleFillSymbol, TextSymbol, Draw,  Accessor) {
        var measureConfig = {
            fillSymbol: new SimpleFillSymbol({
                color: [102, 0, 255, 0.2],
                outline: {
                    color: "#6600FF",
                    width: 2
                }
            }),
        };
        var measureArea = Accessor.createSubclass({
            declaredClass: "dse.modules.measure._measureArea",
            properties: {
                view: {
                    value:null,
                    set:function(value){
                        var draw = new Draw({
                            view: value
                        });
                        this._set("view",value);
                        this._set("draw",draw);
                    }
                },
                type: {
                    readOnly: true,
                    get: function () {
                        return "area";
                    }
                },
                draw: {
                    readOnly: true,
                    value:null,
                }
            },

            //开始测量面积
            start: function () {
                this.view.focus();
                var action = this.draw.create("polygon");
                action.on("vertex-add", function (evt) {
                    this._measure(evt.vertices);

                }.bind(this));

                action.on("cursor-update", function (evt) {
                    this._measure(evt.vertices);
                }.bind(this));

                action.on("draw-complete", function (evt) {
                    this._measure(evt.vertices);
                }.bind(this));

                action.on("vertex-remove", function (evt) {
                    this._measure(evt.vertices);
                }.bind(this));

            },

            clear: function () {
                this.view.graphics.removeAll();
                this.draw.reset();
            },

            _measure: function (vertices) {
                var polygon=this._createPolygon(vertices);
                var area = this._measureArea(polygon);
                if (area < 0) {
                    var simplifiedPolygon = geometryEngine.simplify(polygon);
                    if (simplifiedPolygon) {
                        area = this._measure(simplifiedPolygon);
                    }
                }
                this._labelArea(polygon.centroid,area);
            },

            _createPolygon:function(vertices){
                this.view.graphics.removeAll();
                var polygon = new Polygon({
                    rings: vertices,
                    spatialReference: this.view.spatialReference
                });
                var graphic = this._createGraphic(polygon);
                this.view.graphics.add(graphic);
                return polygon;
            },

            _labelArea:function(geom,area){
                var textSymbol = this._createTextSymbol("面积");
                if (area > 0 && area >= 1000000) {
                    textSymbol.text = (area / 1000000).toFixed(2) + "平方千米";
                } else if (area > 0 && area < 1000000) {
                    textSymbol.text = area.toFixed() + "平方米";
                }
                var txtGraphic=new Graphic({
                    geometry: geom,
                    symbol: textSymbol
                });
                txtGraphic.flag="label";
                this.view.graphics.add(txtGraphic);
            },

            _createGraphic:function(polygon){
                this._remove("polygon");
                var graphic = new Graphic({
                    geometry: polygon,
                    symbol: measureConfig.fillSymbol
                });
                graphic.flag="polygon";
                this.view.graphics.add(graphic);
                return graphic;
            },

            _createTextSymbol: function (text) {
                var textSymbol = new TextSymbol({
                    color: "#FF5722",
                    haloColor: "#33cc33",
                    haloSize: 1,
                    horizontalAlignment: "right",
                    xoffset: 3,
                    yoffset: 3,
                    text: text,
                    font: {  // autocast as esri/symbols/Font
                        size: 12,
                        family: "sans-serif",
                        weight: "bolder"
                    }
                });
                return textSymbol;
            },
            _measureArea: function (polygon) {

                if (this.view.spatialReference.isWGS84 || this.view.spatialReference.isWebMercator) {//在web麦卡托投影和WGS84坐标系下的计算方法
                    return geometryEngine.geodesicArea(polygon, "square-meters");
                } else {//在其他投影坐标系下的计算方法
                    return geometryEngine.planarArea(polygon, "square-meters")
                }
            },
            _remove:function(flag){
                var find = this.view.graphics.find(function(graphic) {
                    return graphic.flag === flag;
                });

                if (find) {
                    this.view.graphics.remove(find);
                }
            },
        });
        return measureArea;
    });
